package it.zqdszb.library.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import it.zqdszb.library.basemvp.BaseModel;
import it.zqdszb.library.basemvp.BasePresenter;
import it.zqdszb.library.utils.ParameterizedTypeUtil;
import it.zqdszb.library.utils.utils.RxDataTool;

/**
 * Created by GH on 2018-05-13.
 * 用ViewPager的时候用懒加载
 */

public abstract class BaseLazyMVPFragment<P extends BasePresenter, M extends BaseModel> extends BaseFragment {

	protected P presenter;

	protected boolean isFragmentVisible = false;
	protected boolean isFirstVisible = false;
	private boolean isReuseView = false;
	protected List<Object> datas =new ArrayList<>();
	protected  View  parentView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		presenter = ParameterizedTypeUtil.init(this, this, getThisActivity());
		initVariable();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		//setUserVisibleHint()有可能在fragment的生命周期外被调用
		if (parentView == null) {
			return;
		}
		if (isFirstVisible && isVisibleToUser) {
			onFragmentFirstVisible();
			isFirstVisible = false;
		}
		if (isVisibleToUser) {
			onFragmentVisibleChange(true);
			isFragmentVisible = true;
			return;
		}
		if (isFragmentVisible) {
			isFragmentVisible = false;
			onFragmentVisibleChange(false);
		}
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		//如果setUserVisibleHint()在rootView创建前调用时，那么
		//就等到rootView创建完后才回调onFragmentVisibleChange(true)
		//保证onFragmentVisibleChange()的回调发生在rootView创建完成之后，以便支持ui操作
		if (parentView == null) {
			parentView = view;
			if (getUserVisibleHint()) {
				if (isFirstVisible) {
					onFragmentFirstVisible();
					isFirstVisible = false;
				}
				onFragmentVisibleChange(true);
				isFragmentVisible = true;
			}
		}else{
			if(RxDataTool.isListEmpty(datas)){
				onFragmentFirstVisible();
			}
		}
		super.onViewCreated(isReuseView && rootView != null ? rootView : view, savedInstanceState);
	}

	/**
	 * 在fragment首次可见时回调，可用于加载数据，防止每次进入都重复加载数据
	 */
	protected void onFragmentFirstVisible() {
		initView();
	}

	/**
	 * 去除setUserVisibleHint()多余的回调场景，保证只有当fragment可见状态发生变化时才回调
	 * 回调时机在view创建完后，所以支持ui操作，解决在setUserVisibleHint()里进行ui操作有可能报null异常的问题
	 *
	 * 可在该回调方法里进行一些ui显示与隐藏
	 *
	 * @param isVisible true  不可见 -> 可见
	 *                  false 可见  -> 不可见
	 */
	protected void onFragmentVisibleChange(boolean isVisible) {

	}

	protected   abstract  void initView();

	protected boolean isFragmentVisible() {
		return isFragmentVisible;
	}


	private void initVariable() {
		isLazy=true;
		isFirstVisible = true;
		isFragmentVisible = false;
		parentView = null;
		isReuseView = true;
	}

	@Override
	public void onDestroy() {
		if (presenter != null) presenter.onDettach();
		initVariable();
		super.onDestroy();
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}

}
